package com.tourism.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tourism.dao.UserDao;
import com.tourism.entity.User;
import com.tourism.entity.UserQuery;
import com.tourism.exception.ExceptionEnum;
import com.tourism.exception.GlobalException;
import com.tourism.service.UserService;
import com.tourism.util.MailUtils;
import com.tourism.util.SystemHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description
 * @Author z
 * @Date 2021-03-01 21:05
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    @Autowired
    private MailUtils mailUtils;

    @Override
    public User getUserById(Long id) {
        return userDao.getUserById(id);
    }
//删除用户
    @Override
    public Boolean deleteUserById(long id) {
        return userDao.deleteUserById(id);
    }

    @Override
    public User getUserByUsername(String username) {
        return userDao.getUserByUsername(username);
    }

    @Override
    public boolean addUser(User user) throws GlobalException {
        //判断用户名是否存在
        if (existUser(user.getUsername(), null)) {
            throw new GlobalException(ExceptionEnum.UserExist);
        }
        Boolean result = mailUtils.checkCode(user.getEmail(), user.getVerifyCode());
        if (result == null) {
            throw new GlobalException(ExceptionEnum.VerifyExpire);
        }
        if (!result) {
            throw new GlobalException(ExceptionEnum.VerifyError);
        }
        user.init();
        return userDao.addUser(user);
    }

    @Override
    public boolean existUser(String username, Long id) {
        boolean exist = false;
        User user = userDao.getUserByUsername(username);
        if (user == null) {
            return exist;
        }
        exist = !user.getId().equals(id);
        return exist;
    }

    @Override
    public boolean updateUserFreeze(Long id, Boolean freeze) throws GlobalException {
        if (!SystemHelper.isAdmin()) {
            throw new GlobalException(ExceptionEnum.NotOperate);
        }
        return userDao.updateUserFreeze(id, freeze);
    }

    @Override
    public boolean updateUser(User user) throws GlobalException {
        if (existUser(user.getUsername(), user.getId())) {
            throw new GlobalException(ExceptionEnum.UserExist);
        }
        return userDao.updateUser(user);
    }

    @Override
    public boolean updateUserPassword(Long id, String password, String newPassword) throws GlobalException {
        User user = userDao.getUserById(id);
        if (!user.getPassword().equals(password)) {
            throw new GlobalException(ExceptionEnum.PasswordError);
        }
        return userDao.updateUserPassword(id, newPassword);
    }

    @Override
    public PageInfo<User> getUserList(UserQuery userQuery) throws GlobalException {
        if (!SystemHelper.isAdmin()) {
            throw new GlobalException(ExceptionEnum.NotOperate);
        }
        //关键步骤
        PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());
        List<User> lineList = userDao.getUserList(userQuery);
        PageInfo<User> result = new PageInfo<>(lineList);
        return result;
    }


}
